import React from 'react';
import router from 'umi/router';

import { connect } from 'dva';

import { Button, Form, Input, Select } from 'antd';

const FormItem = Form.Item;

const { Option } = Select;

const linePath = '/AccountConfig/AccountJournal';


@connect(({
  accountJournal, accountAccount,resPartnerBank
}) => ({
  accountJournal, accountAccount,resPartnerBank

}))
class FormCreate extends React.Component {
  state = {
  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountJournal/view',
        payload:  { id: this.id }
      });

    }

    // 获取多对一字段的下拉列表框选项所需要的数据

    const { dispatch } = this.props;
    await dispatch({
      type: 'accountAccount/search',
      payload: {domain:[], fields:{} },
    });

    await dispatch({
      type: 'resPartnerBank/search',
      payload: {domain:[], fields:{} },
    });


  }

  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();

    const create_or_write = async (vals) => {
      if(this.id){
        const { bank_account_id } = vals;

        if(!bank_account_id){
          delete vals.bank_account_id;
        }

        await dispatch({
          type: 'accountJournal/write',
          payload : {
            id: this.id,  vals,
          },
        });

        router.push(`${linePath}?id=${this.id}`);
      }
      else{

        const { bank_account_id } = vals;

        if(!bank_account_id){
          delete vals.bank_account_id;
        }


        await dispatch({
          type: 'accountJournal/create',
          payload : {
            vals,
          },
        });

        const { accountJournal:{record} } = this.props;

        if (record.id){
          router.push(`${linePath}?id=${record.id}`);
        }
      }

    }


    form.validateFieldsAndScroll( async (err, values) => {

      if (!err) {

        const vals = { ...values,
        };

        await create_or_write(vals)

      }
    });
  };


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { accountJournal: { record } } = this.props;
    const { accountAccount: { recordsList: accountList } } = this.props;
    const { resPartnerBank: { recordsList: partnerBankList } } = this.props;

    return (
      <div>
          <Form onSubmit={this.handleSubmit}>
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="名称">
              {getFieldDecorator('name', {
                rules: [{ required: true }],
                initialValue: record.name || '',
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="代码">
              {getFieldDecorator('code', {
                rules: [{ required: true }],
                initialValue: record.code,
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="类型">
              {getFieldDecorator('type', {
                rules: [{ required: true }],
                initialValue: record.type || 'general'
              })(
                <Select placeholder="" >
                  <Option value={'sale'} >销售账簿</Option>
                  <Option value={'purchase'} >采购账簿</Option>
                  <Option value={'cash'} >现金账簿</Option>
                  <Option value={'bank'} >银行账簿</Option>
                  <Option value={'general'} >杂项账簿</Option>
                </Select>

              )}
            </FormItem>


            <FormItem label="默认借方科目">
              {getFieldDecorator('default_debit_account_id', {
                rules: [{ required: true }],
                initialValue: record.default_debit_account_id ? record.default_debit_account_id.id : 0
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>



            <FormItem label="默认贷方科目">
              {getFieldDecorator('default_credit_account_id', {
                rules: [{ required: true }],
                initialValue: record.default_credit_account_id ? record.default_credit_account_id.id : 0
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="银行账号">
              {getFieldDecorator('bank_account_id', {
                rules: [{ required: true }],
                initialValue: record.bank_account_id ? record.bank_account_id.id : 0
              })(
                <Select placeholder="" >
                  <Option value={0} >未选择</Option>
                  {partnerBankList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.bank_name} ${item.acc_number}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>


          </Form>


      </div>

    );
  }
}


export default Form.create()(FormCreate);


